package com.wzcl.app.service.impl;

import com.wzcl.app.dao.model.WorkShow;
import com.wzcl.app.dao.model.WorkShowReviews;
import com.wzcl.app.dao.repository.AppUserRepository;
import com.wzcl.app.dao.repository.WorkShowRepository;
import com.wzcl.app.dao.repository.WorkShowReviewsRepository;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.WorkShowReviewsDto;
import com.wzcl.app.service.WorkShowReviewsService;
import com.wzcl.app.utils.BeanUtil;
import com.wzcl.app.utils.CollectionUtil;
import com.wzcl.app.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author beingven
 * @Description TODO
 * @createTime 2021年01月12日 17:46:00
 */
@Service
public class WorkShowReviewsServiceImpl implements WorkShowReviewsService {
    @Resource
    private WorkShowReviewsRepository workShowReviewsRepository;
    @Resource
    private AppUserRepository appUserRepository;
    @Resource
    private WorkShowRepository workShowRepository;

    @Override
    @Transactional
    public WorkShowReviews createReviews(WorkShowReviewsDto workShowReviewsDto) {
        WorkShowReviews workShowReviews = BeanUtil.convertBean(workShowReviewsDto, new WorkShowReviews().getClass());
        String imageList = CollectionUtil.getStringFromList(workShowReviewsDto.getImages(), ",");
        workShowReviews.setAppUser(appUserRepository.findAllById(workShowReviewsDto.getUserId()));
        workShowReviews.setWorkShow(workShowRepository.findWorkShowById(workShowReviewsDto.getWorkShowId()));
        workShowReviews.setImageList(imageList);
        workShowReviews.setCreateTime(DateUtil.getDateBySystemTime(System.currentTimeMillis()));
        WorkShowReviews showReviews = workShowReviewsRepository.save(workShowReviews);
        //修改作品秀为已点评
        workShowRepository.updateCommentStatusById(1, workShowReviewsDto.getWorkShowId());
        return showReviews;
    }

    @Override
    public RVO deleteReviewsById(Integer id) {
        if (id == null){
            return RVO.error("id不能为空");
        }
        workShowReviewsRepository.deleteById(id);
        return RVO.success("删除点评成功");
    }

    @Override
    public RVO retrieveById(Integer id) {
        if (id == null){
            return RVO.error("id不能为空");
        }
        WorkShowReviews workShowReviews = workShowReviewsRepository.findReviewsById(id);
        return RVO.success(workShowReviews);
    }
}
